package com.example.suhui_test.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.suhui_test.R;

/**
 * 圆形进度条自定义View
 * 知识点：
 * 1. Canvas绘制 - 绘制圆形、弧形、文字
 * 2. Paint的使用 - 设置颜色、样式、抗锯齿等
 * 3. 自定义属性 - 通过XML设置自定义属性
 * 4. onMeasure方法 - 测量View尺寸
 * 5. onDraw方法 - 绘制View内容
 */
public class CircleProgressView extends View {
    
    // 默认值
    private static final int DEFAULT_PROGRESS_COLOR = 0xFF2196F3;
    private static final int DEFAULT_BACKGROUND_COLOR = 0xFFE0E0E0;
    private static final int DEFAULT_TEXT_COLOR = 0xFF333333;
    private static final float DEFAULT_STROKE_WIDTH = 20f;
    private static final int DEFAULT_TEXT_SIZE = 48;
    
    // 自定义属性
    private int progressColor;
    private int backgroundColor;
    private int textColor;
    private float strokeWidth;
    private boolean showText;
    private int textSize;
    
    // 进度相关
    private int progress = 0; // 0-100
    private int maxProgress = 100;
    
    // 绘制相关
    private Paint backgroundPaint;
    private Paint progressPaint;
    private Paint textPaint;
    private RectF rectF;
    
    // View尺寸
    private int centerX;
    private int centerY;
    private int radius;

    public CircleProgressView(Context context) {
        this(context, null);
    }

    public CircleProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttributes(context, attrs);
        initPaint();
    }

    /**
     * 初始化自定义属性
     */
    private void initAttributes(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
        
        progressColor = typedArray.getColor(R.styleable.CircleProgressView_progressColor, DEFAULT_PROGRESS_COLOR);
        backgroundColor = typedArray.getColor(R.styleable.CircleProgressView_backgroundColor, DEFAULT_BACKGROUND_COLOR);
        textColor = typedArray.getColor(R.styleable.CircleProgressView_textColor, DEFAULT_TEXT_COLOR);
        strokeWidth = typedArray.getDimension(R.styleable.CircleProgressView_strokeWidth, DEFAULT_STROKE_WIDTH);
        showText = typedArray.getBoolean(R.styleable.CircleProgressView_showText, true);
        textSize = typedArray.getDimensionPixelSize(R.styleable.CircleProgressView_textSize, DEFAULT_TEXT_SIZE);
        
        typedArray.recycle();
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        // 背景圆环画笔
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setStyle(Paint.Style.STROKE);
        backgroundPaint.setStrokeWidth(strokeWidth);
        backgroundPaint.setColor(backgroundColor);
        backgroundPaint.setStrokeCap(Paint.Cap.ROUND);
        
        // 进度圆环画笔
        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(strokeWidth);
        progressPaint.setColor(progressColor);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        
        // 文字画笔
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(textSize);
        textPaint.setColor(textColor);
        textPaint.setTextAlign(Paint.Align.CENTER);
        
        // 绘制区域
        rectF = new RectF();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        
        // 获取测量后的宽高
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        
        // 计算中心点和半径
        centerX = width / 2;
        centerY = height / 2;
        radius = Math.min(centerX, centerY) - (int) strokeWidth / 2;
        
        // 设置绘制区域
        rectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        // 绘制背景圆环
        canvas.drawCircle(centerX, centerY, radius, backgroundPaint);
        
        // 绘制进度圆弧
        if (progress > 0) {
            float sweepAngle = 360f * progress / maxProgress;
            canvas.drawArc(rectF, -90, sweepAngle, false, progressPaint);
        }
        
        // 绘制进度文字
        if (showText) {
            String progressText = progress + "%";
            
            // 计算文字的基线位置
            Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
            float baseline = centerY - (fontMetrics.top + fontMetrics.bottom) / 2;
            
            canvas.drawText(progressText, centerX, baseline, textPaint);
        }
    }

    /**
     * 设置进度
     * @param progress 进度值 (0-100)
     */
    public void setProgress(int progress) {
        this.progress = Math.max(0, Math.min(progress, maxProgress));
        invalidate(); // 重新绘制
    }

    /**
     * 获取当前进度
     */
    public int getProgress() {
        return progress;
    }

    /**
     * 设置最大进度
     */
    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
        invalidate();
    }

    /**
     * 设置进度颜色
     */
    public void setProgressColor(int color) {
        this.progressColor = color;
        progressPaint.setColor(color);
        invalidate();
    }

    /**
     * 设置背景颜色
     */
    public void setBackgroundColor(int color) {
        this.backgroundColor = color;
        backgroundPaint.setColor(color);
        invalidate();
    }

    /**
     * 设置是否显示文字
     */
    public void setShowText(boolean showText) {
        this.showText = showText;
        invalidate();
    }

    /**
     * 设置线条宽度
     */
    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        backgroundPaint.setStrokeWidth(strokeWidth);
        progressPaint.setStrokeWidth(strokeWidth);
        invalidate();
    }
}